﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

using Microsoft.ApplicationBlocks.Data;

using YouHP.IDAL;
using YouHP.Model;

namespace YouHP.SqlServerDAL
{
    public class UserActionDAL : IUserAction
    {
        #region basic operations
        public bool AddUserAction(UserActionInfo info)
        {
            return SqlHelper.ExecuteNonQuery(Common.ConnStr,
                Common.UserActionSP_AddUserAction,
                info.UserId,
                info.UserName,
                info.ActionTime,
                info.ActionType,
                info.ActionContent
                ) > 0;
        }
        public bool DeleteUserAction(int[] ids)
        {
            int cnt = ids.Length;
            if (cnt <= 0)
                return false;

            StringBuilder sb = new StringBuilder();
            sb.Append("[id] in (");
            sb.Append(ids[0]);

            for (int i = 1; i < cnt; i++)
            {
                sb.Append(", ");
                sb.Append(ids[i]);
            }
            sb.Append(")");

            return SqlHelper.ExecuteNonQuery(Common.ConnStr,
                Common.UserActionSP_DelUserAction,
                sb.ToString()) >= cnt;
        }
        #endregion

        #region list and search operations
        private List<UserActionInfo> GetUserActionList(int pageSize, int pageIndex, string condStr, string orderStr, out int count)
        {
            DataSet dset = SqlHelper.ExecuteDataset(Common.ConnStr,
                Common.GetFullName(Common.UserActionSP_GetUserActionList),
                pageSize, pageIndex, condStr, orderStr);

            IDataReader dr = dset.Tables[0].CreateDataReader();
            if (dr != null && dr.Read())
            {
                count = dr.GetInt32(0);
                dr.Close();
            }
            else
            {
                count = 0;
            }
            dr = dset.Tables[1].CreateDataReader();

            return CBO.FillCollection<UserActionInfo>(dr);
        }

        public List<UserActionInfo> GetLatestUserActionList(int pageSize, int pageIndex, int days, out int count)
        {
            string condStr = string.Format(@" DATEDIFF(day, [UserAction].[ActionTime], getdate()) <= {0} AND [UserAction].[ActionType] in (2,3,4,5,7,8,9,10,11,12)", days);
            return GetUserActionList(pageSize, pageIndex, condStr, " [UserAction].[Id] desc ", out count);
        }

        public List<UserActionInfo> GetUserActionByUserId(int pageSize, int pageIndex, int uid, int days, out int count)
        {
            string condStr = string.Format(@" DATEDIFF(day, [UserAction].[ActionTime], getdate()) <= {0} AND [UserAction].[UserId] = {1}", days,uid);
            return GetUserActionList(pageSize, pageIndex, condStr, "[UserAction].[Id] desc ", out count);
        }



        public List<UserActionInfo> GetFriendActionList(int pageSize, int pageIndex, int uid, int days, out int count)
        {
            string condStr = string.Format(@" [Friend].[HostUserId] = {0} AND [Friend].[State] = 2 AND DATEDIFF(day, [UserAction].[ActionTime], getdate()) <= {1} ", uid, days);
            DataSet dset = SqlHelper.ExecuteDataset(Common.ConnStr,
               Common.GetFullName(Common.UserActionSP_GetFriendActionList),
               pageSize, pageIndex, condStr, "[UserAction].[Id] desc ");

            IDataReader dr = dset.Tables[0].CreateDataReader();
            if (dr != null && dr.Read())
            {
                count = dr.GetInt32(0);
                dr.Close();
            }
            else
            {
                count = 0;
            }
            dr = dset.Tables[1].CreateDataReader();

            return CBO.FillCollection<UserActionInfo>(dr);
        }


        public List<UserActionInfo> GetAllFriendActionList(int uid, int days)
        {
            string condStr = string.Format(@" [Friend].[HostUserId] = {0} AND [Friend].[State] = 2 AND DATEDIFF(day, [UserAction].[ActionTime], getdate()) <= {1} ", uid, days);

            IDataReader reader = SqlHelper.ExecuteReader(Common.ConnStr,
                Common.UserActionSP_GetAllFriendActionList,
                condStr, "[UserAction].[Id] desc ") as IDataReader;

            return CBO.FillCollection<UserActionInfo>(reader);
        }


        public List<UserActionInfo> GetPatientsLikeMeActionList(int uid,int diseaseId, int days)
        {

            string condStr = string.Format(@" [patientrecord].[patientid] != {0} AND [patientrecord].[diseaseid] = {1} AND DATEDIFF(day, [UserAction].[ActionTime], getdate()) <= {2} ",uid,diseaseId,days);
            IDataReader reader = SqlHelper.ExecuteReader(Common.ConnStr,
               Common.UserActionSP_GetPatientsLikeMeActionList,
               condStr, "[UserAction].[Id] desc ") as IDataReader;

            return CBO.FillCollection<UserActionInfo>(reader);
            
        }

        #endregion
    }
}

